package com.fresh.fresh.controller;

import com.fresh.fresh.dao.Commodity;
import com.fresh.fresh.dao.Cart;
import com.fresh.fresh.service.CommodityService;
import com.fresh.fresh.service.CartService;
import com.fresh.fresh.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/cart")
@Api(tags = "购物车接口")
public class CartController {

    @Autowired
    CartService cartService;

    @Autowired
    CommodityService commodityService;

    /*根据用户id获取当前用户的购物车*/
    @GetMapping("/id={id}")
    @ApiOperation("获取当前用户购物车接口")
    public Result<?> getShoppingCart(@PathVariable Integer id) {
        List<Cart> data = cartService.list(id);
        return Result.success(data);
    }

    @DeleteMapping("/delete/id={id}")
    @ApiOperation("删除购物车接口")
    public Result<?> deleteCommodity(@PathVariable Integer id) {
        Integer del = cartService.deleteCommodity(id);
        return Result.success(del);
    }

    @PostMapping("/addCart")
    @ApiOperation("增加购物车接口")
    public Result<?> addShopping(@RequestBody Cart shopping) {
        cartService.addShopping(shopping);
        return Result.success("成功");
    }

    /*根据购物车的id来获取对应的商品*/
    @GetMapping("/getCart")
    @ApiOperation("获取购物车信息接口")
    public Result<?> getShopping(@RequestParam("userId") Integer userId,@RequestParam("commodityId")Integer commodityId) {
      List<Cart>  data = cartService.getShopping(userId,commodityId);
        return Result.success(data);
    }


}
